package datastructre;

import java.util.LinkedList;

public class MaxFindStack {
    private LinkedList<Integer> mainStack=new LinkedList<>();
    private LinkedList<Integer> maxStack=new LinkedList<>();

    public MaxFindStack() {
    }

    public void push(int element){
        mainStack.push(element);
        if (maxStack.isEmpty()) {
            maxStack.push(element);
        } else {
            maxStack.push(Math.max(maxStack.peek(),element));
        }
    }

    public int findMax() {
        return maxStack.peek();
    }

    public int pop() {
        maxStack.pop();
        return mainStack.pop();
    }

    @Override
    public String toString() {
        return "MaxFindStack{" +
                "mainStack=" + mainStack +
                ", maxStack=" + maxStack +
                '}';
    }

    public static void main(String[] args) {
        MaxFindStack stack = new MaxFindStack();
        stack.push(1);
        stack.push(2);
        stack.push(7);
        stack.push(5);
        stack.push(4);
        System.out.println("stack = " + stack);
        stack.pop();
        stack.pop();
        System.out.println("stack = " + stack);
    }

}
